home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / gspath.c < prev    next >
C/C++ Source or Header  |  1993-05-13  |  9KB  |  301 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gspath.c */
  20. /* Path construction routines for Ghostscript library */
  21. #include "math_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gxfixed.h"
  25. #include "gxmatrix.h"
  26. #include "gxpath.h"
  27. #include "gzstate.h"
  28.  
  29. /* Conversion parameters */
  30. #define degrees_to_radians (M_PI / 180.0)
  31.  
  32. /* ------ Miscellaneous ------ */
  33.  
  34. int
  35. gs_newpath(gs_state *pgs)
  36. {    gx_path_release(pgs->path);
  37.     gx_path_init(pgs->path, pgs->memory_procs);
  38.     return 0;
  39. }
  40.  
  41. int
  42. gs_closepath(gs_state *pgs)
  43. {    return gx_path_close_subpath(pgs->path);
  44. }
  45.  
  46. int
  47. gs_upmergepath(gs_state *pgs)
  48. {    return gx_path_add_path(pgs->saved->path, pgs->path);
  49. }
  50.  
  51. /* ------ Points and lines ------ */
  52.  
  53. int
  54. gs_currentpoint(const gs_state *pgs, gs_point *ppt)
  55. {    gs_fixed_point pt;
  56.     int code = gx_path_current_point(pgs->path, &pt);
  57.     if ( code < 0 ) return code;
  58.     return gs_itransform(pgs, fixed2float(pt.x), fixed2float(pt.y), ppt);
  59. }
  60.  
  61. int
  62. gs_moveto(gs_state *pgs, floatp x, floatp y)
  63. {    int code;
  64.     gs_fixed_point pt;
  65.     if ( (code = gs_point_transform2fixed(&pgs->ctm, x, y, &pt)) >= 0 )
  66.         code = gx_path_add_point(pgs->path, pt.x, pt.y);
  67.     return code;
  68. }
  69.  
  70. int
  71. gs_rmoveto(gs_state *pgs, floatp x, floatp y)
  72. {    int code;
  73.     gs_fixed_point dpt;
  74.     if ( (code = gs_distance_transform2fixed(&pgs->ctm, x, y, &dpt)) >= 0 )
  75.         code = gx_path_add_relative_point(pgs->path, dpt.x, dpt.y);
  76.     return code;
  77. }
  78.  
  79. int
  80. gs_lineto(gs_state *pgs, floatp x, floatp y)
  81. {    int code;
  82.     gs_fixed_point pt;
  83.     if ( (code = gs_point_transform2fixed(&pgs->ctm, x, y, &pt)) >= 0 )
  84.         code = gx_path_add_line(pgs->path, pt.x, pt.y);
  85.     return code;
  86. }
  87.  
  88. int
  89. gs_rlineto(gs_state *pgs, floatp x, floatp y)
  90. {    gs_fixed_point cpt, dpt;
  91.     int code = gx_path_current_point(pgs->path, &cpt);
  92.     if ( code < 0 ) return code;
  93.     if ( (code = gs_distance_transform2fixed(&pgs->ctm, x, y, &dpt)) >= 0 )
  94.         code = gx_path_add_line(pgs->path, cpt.x + dpt.x, cpt.y + dpt.y);
  95.     return code;
  96. }
  97.  
  98. /* ------ Arcs ------ */
  99.  
  100. /* Forward declarations */
  101. /*
  102.  * Because of an obscure bug in the IBM RS/6000 compiler, the int argument
  103.  * for arc_either and arc_add must come before the floatp arguments.
  104.  */
  105. private int arc_either(P7(gs_state *, int,
  106.   floatp, floatp, floatp, floatp, floatp));
  107. private int arc_add(P9(gs_state *, int,
  108.   floatp, floatp, floatp, floatp, floatp, floatp, floatp));
  109.  
  110. int
  111. gs_arc(gs_state *pgs,
  112.   floatp xc, floatp yc, floatp r, floatp ang1, floatp ang2)
  113. {    return arc_either(pgs, 0, xc, yc, r, ang1, ang2);
  114. }
  115.  
  116. int
  117. gs_arcn(gs_state *pgs,
  118.   floatp xc, floatp yc, floatp r, floatp ang1, floatp ang2)
  119. {    return arc_either(pgs, 1, xc, yc, r, ang1, ang2);
  120. }
  121.  
  122. private int
  123. arc_either(gs_state *pgs, int clockwise,
  124.   floatp axc, floatp ayc, floatp arad, floatp aang1, floatp aang2)
  125. {    float ar = arad;
  126.     fixed ang1 = float2fixed(aang1), ang2 = float2fixed(aang2), adiff;
  127.     float ang1r;
  128.     float x0, y0, sin0, cos0;
  129.     float x3r, y3r;
  130.     int first = 1;
  131.     int code;
  132.     if ( ar < 0 )
  133.        {    ang1 += int2fixed(180);
  134.         ang2 += int2fixed(180);
  135.         ar = - ar;
  136.        }
  137. #define fixed90 int2fixed(90)
  138. #define fixed360 int2fixed(360)
  139.     ang1r = fixed2float(ang1 % fixed360) * degrees_to_radians;
  140.     sin0 = ar * sin(ang1r), cos0 = ar * cos(ang1r);
  141.     x0 = axc + cos0, y0 = ayc + sin0;
  142.     if ( clockwise )
  143.        {    /* Quadrant reduction */
  144.         while ( ang1 < ang2 ) ang2 -= fixed360;
  145.         while ( (adiff = ang2 - ang1) < -fixed90 )
  146.            {    float w = sin0; sin0 = -cos0; cos0 = w;
  147.             x3r = axc + cos0, y3r = ayc + sin0;
  148.             code = arc_add(pgs, first, ar, x0, y0, x3r, y3r,
  149.                 (x0 + cos0),
  150.                 (y0 + sin0));
  151.             if ( code < 0 ) return code;
  152.             x0 = x3r, y0 = y3r;
  153.             ang1 -= fixed90;
  154.             first = 0;
  155.            }
  156.        }
  157.     else
  158.        {    /* Quadrant reduction */
  159.         while ( ang2 < ang1 ) ang2 += fixed360;
  160.         while ( (adiff = ang2 - ang1) > fixed90 )
  161.            {    float w = cos0; cos0 = -sin0; sin0 = w;
  162.             x3r = axc + cos0, y3r = ayc + sin0;
  163.             code = arc_add(pgs, first, ar, x0, y0, x3r, y3r,
  164.                 (x0 + cos0),
  165.                 (y0 + sin0));
  166.             if ( code < 0 ) return code;
  167.             x0 = x3r, y0 = y3r;
  168.             ang1 += fixed90;
  169.             first = 0;
  170.            }
  171.        }
  172.     /* Compute the intersection of the tangents. */
  173.        {    double trad = tan(fixed2float(adiff) * (degrees_to_radians / 2));
  174.         float ang2r = fixed2float(ang2) * degrees_to_radians;
  175.         code = arc_add(pgs, first, ar, x0, y0,
  176.             (axc + ar * cos(ang2r)),
  177.             (ayc + ar * sin(ang2r)),
  178.             (x0 - trad * sin0),
  179.             (y0 + trad * cos0));
  180.        }
  181.     return code;
  182. }
  183.  
  184. int
  185. gs_arcto(gs_state *pgs,
  186.   floatp ax1, floatp ay1, floatp ax2, floatp ay2, floatp arad,
  187.   float *retxy)            /* float retxy[4] */
  188. {    float xt0, yt0, xt2, yt2;
  189.     gs_point up0;
  190. #define ax0 up0.x
  191. #define ay0 up0.y
  192.     int code;
  193.     if ( arad < 0 )
  194.         return_error(gs_error_undefinedresult);
  195.     /* Transform the current point back into user coordinates */
  196.     if ( (code = gs_currentpoint(pgs, &up0)) < 0 ) return code;
  197.        {    /* Now we have to compute the tangent points. */
  198.         /* Basically, the idea is to compute the tangent */
  199.         /* of the bisector by using tan(x+y) and tan(z/2) */
  200.         /* formulas, without ever using any trig. */
  201.         float dx0 = ax0 - ax1, dy0 = ay0 - ay1;
  202.         float dx2 = ax2 - ax1, dy2 = ay2 - ay1;
  203.         /* Compute the squared lengths from p1 to p0 and p2. */
  204.         double sql0 = dx0 * dx0 + dy0 * dy0;
  205.         double sql2 = dx2 * dx2 + dy2 * dy2;
  206.         /* Compute the distance from p1 to the tangent points. */
  207.         /* This is the only hairy part. */
  208.         double num = dy0 * dx2 - dy2 * dx0;
  209.         double denom = sqrt(sql0 * sql2) - (dx0 * dx2 + dy0 * dy2);
  210.         /* Check for collinear points. */
  211.         if ( fabs(num) < 1.0e-6 || fabs(denom) < 1.0e-6 )
  212.            {    gs_fixed_point pt;
  213.             code = gs_point_transform2fixed(&pgs->ctm, ax1, ay1, &pt);
  214.             if ( code >= 0 ) code = gx_path_add_line(pgs->path, pt.x, pt.y);
  215.             xt0 = xt2 = ax1;
  216.             yt0 = yt2 = ay1;
  217.            }
  218.         else        /* not collinear */
  219.            {    double dist = fabs(arad * num / denom);
  220.             double l0 = dist / sqrt(sql0), l2 = dist / sqrt(sql2);
  221.             xt0 = ax1 + dx0 * l0;
  222.             yt0 = ay1 + dy0 * l0;
  223.             xt2 = ax1 + dx2 * l2;
  224.             yt2 = ay1 + dy2 * l2;
  225.             code = arc_add(pgs, 1, arad, xt0, yt0, xt2, yt2, ax1, ay1);
  226.            }
  227.        }
  228.     if ( retxy != 0 )
  229.        {    retxy[0] = xt0;
  230.         retxy[1] = yt0;
  231.         retxy[2] = xt2;
  232.         retxy[3] = yt2;
  233.        }
  234.     return code;
  235. }
  236.  
  237. /* Internal routine for adding an arc to the path. */
  238. private int
  239. arc_add(gs_state *pgs, int first,
  240.   floatp r, floatp x0, floatp y0, floatp x3, floatp y3, floatp xt, floatp yt)
  241. {    gx_path *path = pgs->path;
  242.     floatp dx = xt - x0, dy = yt - y0;
  243.     floatp fraction;
  244.     gs_fixed_point p0, p3, pt, cpt;
  245.     int code;
  246.     /* Compute the fraction coefficient for the curve. */
  247.     /* See gx_path_add_arc for details. */
  248.     if ( fabs(r) < 1.0e-4 )        /* almost zero radius */
  249.        {    fraction = 0.0;
  250.        }
  251.     else
  252.        {    double ratio2 = (dx * dx + dy * dy) / (r * r);
  253.         fraction = (4.0/3.0) / (1 + sqrt(1 + ratio2));
  254.        }
  255. #ifdef DEBUG
  256. if ( gs_debug['r'] )
  257.     dprintf7("[r]Arc f=%f p0=(%f,%f) pt=(%f,%f) p3=(%f,%f) first=%d\n",
  258.          x0, y0, xt, yt, x3, y3, first);
  259. #endif
  260.     if (    (code = gs_point_transform2fixed(&pgs->ctm, x0, y0, &p0)) < 0 ||
  261.         (code = gs_point_transform2fixed(&pgs->ctm, x3, y3, &p3)) < 0 ||
  262.         (code = gs_point_transform2fixed(&pgs->ctm, xt, yt, &pt)) < 0 ||
  263.         (first && (code = (gx_path_current_point(path, &cpt) >= 0 ?
  264.              gx_path_add_line(path, p0.x, p0.y) :
  265.              gx_path_add_point(path, p0.x, p0.y))) < 0)
  266.        )
  267.         return code;
  268.     return gx_path_add_arc(path, p0.x, p0.y, p3.x, p3.y, pt.x, pt.y, fraction);
  269. }
  270.  
  271. /* ------ Curves ------ */
  272.  
  273. int
  274. gs_curveto(gs_state *pgs,
  275.   floatp x1, floatp y1, floatp x2, floatp y2, floatp x3, floatp y3)
  276. {    gs_fixed_point p1, p2, p3;
  277.     int code;
  278.     if (    (code = gs_point_transform2fixed(&pgs->ctm, x1, y1, &p1)) < 0 ||
  279.         (code = gs_point_transform2fixed(&pgs->ctm, x2, y2, &p2)) < 0 ||
  280.         (code = gs_point_transform2fixed(&pgs->ctm, x3, y3, &p3)) < 0
  281.        ) return code;
  282.     return gx_path_add_curve(pgs->path,
  283.         p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
  284. }
  285.  
  286. int
  287. gs_rcurveto(gs_state *pgs,
  288.   floatp dx1, floatp dy1, floatp dx2, floatp dy2, floatp dx3, floatp dy3)
  289. {    gs_fixed_point pt, p1, p2, p3;
  290.     int code = gx_path_current_point(pgs->path, &pt);
  291.     if ( code < 0 ) return code;
  292.     if (    (code = gs_distance_transform2fixed(&pgs->ctm, dx1, dy1, &p1)) < 0 ||
  293.         (code = gs_distance_transform2fixed(&pgs->ctm, dx2, dy2, &p2)) < 0 ||
  294.         (code = gs_distance_transform2fixed(&pgs->ctm, dx3, dy3, &p3)) < 0
  295.        ) return code;
  296.     return gx_path_add_curve(pgs->path,
  297.         pt.x + p1.x, pt.y + p1.y,
  298.         pt.x + p2.x, pt.y + p2.y,
  299.         pt.x + p3.x, pt.y + p3.y);
  300. }
  301.